package com.yike.order.controller;

import com.yike.common.Response.LklPayCallback;
import com.yike.common.api.ApiResult;
import com.yike.common.pagination.Paging;
import com.yike.order.param.*;
import com.yike.order.service.OrderService;
import com.yike.order.vo.OrderListVo;
import com.yike.order.vo.RefundInfoVo;
import com.yike.order.vo.RefundVo;
import com.yike.order.vo.OrderVo;
import lombok.extern.slf4j.Slf4j;
import com.yike.common.base.BaseController;
import com.yike.common.log.OperationLog;
import com.yike.common.enums.OperationLogType;
import com.yike.common.api.Add;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 订单表 控制器
 *
 * @author yike
 * @since 2024-07-18
 */
@Slf4j
@RestController
@RequestMapping("/order")
@Api(value = "订单API", tags = {"订单管理"})
public class OrderController extends BaseController {

    @Autowired
    private OrderService orderService;



//    @PostMapping("/getLklStatus")
//    @OperationLog(name = "查询订单支付状态(拉卡拉)", type = OperationLogType.ADD)
//    @ApiOperation(value = "查询订单支付状态(拉卡拉)", response = ApiResult.class)
//    public Map<String ,Object> getLklStatus(@Validated @RequestParam String orderNo) throws Exception {
//        return orderService.getLklStatus(orderNo);
//    }

    /**
     * 添加订单表
     */
    @PostMapping("/submit")
    @OperationLog(name = "提交订单", type = OperationLogType.ADD)
    @ApiOperation(value = "提交订单", response = ApiResult.class)
    public ApiResult<String> submit(@Validated(Add.class) @RequestBody AddOrderParam order) throws Exception {
        String orderNo = orderService.saveOrder(order);
        return ApiResult.ok(orderNo);
    }

    @PostMapping("/pay")
    @OperationLog(name = "订单支付", type = OperationLogType.ADD)
    @ApiOperation(value = "订单支付", response = ApiResult.class)
    public ApiResult<Integer> pay(@Validated(Add.class) @RequestBody PayOrderParam payOrderParam) throws Exception {
        //Integer status = orderService.pay(payOrderParam); //微信支付
        Integer status = orderService.lklPay(payOrderParam); //拉卡拉支付

        return ApiResult.ok(status);
    }



    @PostMapping("/refund")
    @OperationLog(name = "订单退款", type = OperationLogType.ADD)
    @ApiOperation(value = "订单退款", response = ApiResult.class)
    public ApiResult<Boolean> refund(@Validated(Add.class) @RequestBody RefundOrderParam refundOrderParam) throws Exception {
        boolean flag = orderService.refund(refundOrderParam);
        return ApiResult.result(flag);
    }

    @GetMapping("/queryStatus/{orderNo}")
    //@OperationLog(name = "订单状态查询", type = OperationLogType.ADD)
    @ApiOperation(value = "订单状态查询", response = ApiResult.class)
    public ApiResult<Integer> queryStatus(@PathVariable("orderNo") String orderNo) throws Exception {
        //Integer status = orderService.queryStatus(orderNo); //  订单状态查询
        Integer status = orderService.getLklStatus(orderNo);
        return ApiResult.ok(status);
    }

    /**
     * 商品分页列
     */
    @PostMapping("/getUserOrderList")
    //@OperationLog(name = "用户订单列表", type = OperationLogType.PAGE)
    @ApiOperation(value = "用户订单列表", response = OrderListVo.class)
    public ApiResult<Paging<OrderListVo>> getUserOrderList(@Validated @RequestBody OrderPageParam orderPageParam) throws Exception {
        Paging<OrderListVo> paging = orderService.getUserOrderList(orderPageParam);
        return ApiResult.ok(paging);
    }

    /**
     * 商品分页列
     */
    @PostMapping("/getShopOrderList")
    //@OperationLog(name = "店铺订单列表", type = OperationLogType.PAGE)
    @ApiOperation(value = "店铺订单列表", response = OrderListVo.class)
    public ApiResult<Paging<OrderListVo>> getShopOrderList(@Validated @RequestBody ShopOrderPageParam shopOrderPageParam) throws Exception {
        Paging<OrderListVo> paging = orderService.getShopOrderList(shopOrderPageParam);
        return ApiResult.ok(paging);
    }

    @PostMapping("/getShopRefundList")
    //@OperationLog(name = "店铺退款列表", type = OperationLogType.PAGE)
    @ApiOperation(value = "店铺退款列表", response = RefundVo.class)
    public ApiResult<Paging<RefundVo>> getShopRefundList(@Validated @RequestBody ShopRefundPageParam shopRefundPageParam) throws Exception {
        Paging<RefundVo> paging = orderService.getShopRefundList(shopRefundPageParam);
        return ApiResult.ok(paging);
    }

    @GetMapping("/info/{orderNo}")
    //@OperationLog(name = "订单详情", type = OperationLogType.INFO)
    @ApiOperation(value = "订单详情", response = OrderVo.class)
    public ApiResult<OrderVo> getOrderInfo(@PathVariable("orderNo") String orderNo) throws Exception {
        OrderVo orderVo = orderService.getOrderInfo(orderNo);
        return ApiResult.ok(orderVo);
    }

    @GetMapping("/refundInfo/{refundNo}")
    //@OperationLog(name = "退款详情", type = OperationLogType.INFO)
    @ApiOperation(value = "退款详情", response = RefundInfoVo.class)
    public ApiResult<RefundInfoVo> getRefundInfo(@PathVariable("refundNo") String refundNo) throws Exception {
        RefundInfoVo refundInfoVo = orderService.getRefundInfo(refundNo);
        return ApiResult.ok(refundInfoVo);
    }

    @GetMapping("/getOrderBindingUrl/{orderNo}")
    @OperationLog(name = "订单绑定URL", type = OperationLogType.INFO)
    @ApiOperation(value = "订单绑定URL", response = ApiResult.class)
    public ApiResult<String> getOrderBindingUrl(@PathVariable("orderNo") String orderNo) throws Exception {
        String url = orderService.getOrderBindingUrl(orderNo);
        return ApiResult.ok(url);
    }

}

